{UTF-8 без BOM}
Uses
	routine, DispProc, Semaphor, Process;
var
	d:TDispatcherProcess;
	T:longint;
	i, N:integer;
	F:text;
	S:TSemaphore;
	pathToFile:string;
	oldMemory, newMemory:	longint;
	main:descptr;
	threads:array[1..4] of body_process;
	numberProcess:integer;
procedure first;
far;
begin
	while true do
		begin
			S.P;
			for i:=1 to N do
				begin
					write(f, 1);
					get_current_dispatcher^.delayTimeProcess(T);
				end;
			S.V;
		end;
end;
procedure second;
far;
begin
	while true do
		begin
			S.P;
			for i:=1 to N do
				begin
					write(f, 2);
					get_current_dispatcher^.delayTimeProcess(T);
				end;
			S.V;
		end;
end;
procedure normal;
far;
begin
	while true do
		begin
	{		writeln('running');}
		end;
end;
procedure manager;
far;
begin
	while true do
		begin
			if (isEscape) then
				begin
					get_current_dispatcher^.stop;
				end;
		end;
end;





(* MAIN *)
begin
	oldMemory:=MemAvail;
	new(main);
	writeln('Enter the path to file for output:');
	readln(pathToFile);
	assign(F, pathToFile);
	rewrite(F);
	writeln('Enter N:');
	readln(N);
	writeln('Enter T:');
	readln(T);
	numberProcess:=4;
	threads[1]:=first;
	threads[2]:=second;
	threads[3]:=normal;
	threads[4]:=manager;
	d.init(numberProcess, threads, main);
	S.init(1);
	set_current_dispatcher(@d);
	newMemory:=MemAvail;
	d.start;
	dispose(main);
	d.done;
	S.done;
	writeln('before the memory allocation: ', oldMemory);
	writeln('after the memory allocation: ', newMemory);
	writeln('after freeing memory: ', memAvail);
	close(f);
end.